package view;

import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;

import javax.swing.JPanel;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;

import model.Computation;

public class SampleGraph extends JPanel{
	private Computation computation;
        
	DefaultCategoryDataset dataset;
    JFreeChart lineChart;
    ChartPanel chartPanel;
        
    public SampleGraph() {
       
        dataset = new DefaultCategoryDataset();
        lineChart = createChart(dataset);
        chartPanel = new ChartPanel(lineChart); 
        chartPanel.setPreferredSize(new Dimension(600, 400));
        add(chartPanel);
            
     }
    private DefaultCategoryDataset createDataset(Computation newComputation) {
    	final String series2 = "Sample";
    	
    	if (computation != null) {
			for (int i = 0; i < computation.getUniqueMeanList().size(); i++) {
	        	dataset.removeValue(series2, computation.getUniqueMeanList().get(i).toString());
	        	
	        }
		}  
    	
    	for(int j = 0; j < newComputation.getUniqueMeanList().size(); j++)
            dataset.addValue(newComputation.getfunctionX().get(j), series2, newComputation.getUniqueMeanList().get(j).toString());
    
    	this.computation = newComputation;
    	
    return dataset;          
    }

    private JFreeChart createChart(final CategoryDataset dataset) {
	    
	    // create the chart...
	    final JFreeChart chart = ChartFactory.createLineChart(
	        "Sample Simulation",       
	        "x",                    
	        "f(x)",                   
	        dataset,                   
	        PlotOrientation.VERTICAL,  
	        true,                      
	        true,                      
	        false                      
	    );
	
	    chart.setBackgroundPaint(Color.white);
	
	    final CategoryPlot plot = (CategoryPlot) chart.getPlot();
	    plot.setBackgroundPaint(Color.lightGray);
	    plot.setRangeGridlinePaint(Color.white);
	
	    // customise the range axis...
	    final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
	    rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
	    rangeAxis.setAutoRangeIncludesZero(true);
	
	    final LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer();
	
	
	    renderer.setSeriesStroke(
	        0, new BasicStroke(
	            2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND,
	            1.0f, new float[] {10.0f, 6.0f}, 0.0f
	        )
	    );
	    renderer.setSeriesStroke(
	        1, new BasicStroke(
	            2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND,
	            1.0f, new float[] {6.0f, 6.0f}, 0.0f
	        )
	    );
	    renderer.setSeriesStroke(
	        2, new BasicStroke(
	            2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND,
	            1.0f, new float[] {2.0f, 6.0f}, 0.0f
	        )
	    );
	    
	    return chart;
	}
    
    public void updateData(Computation computation){
        
        dataset = createDataset(computation);
        lineChart = createChart(dataset);

		chartPanel.setPreferredSize(new Dimension(600, 400));
        chartPanel.setChart(lineChart);
    	}
  
}